﻿using System;
using System.Web.Security;
using System.Web.UI.WebControls;
using FranceTriple.Dal;
using System.Linq;
using System.Web;

namespace TeamFranceTriple
{
    public partial class FriendlyTeam : BaseUserControl
    {
        private static String TsViewerFormat = @"http://cache.www.gametracker.com/components/html0/?host={0}&bgColor=000000&fontColor=ffffff&titleBgColor=000000&titleBgColor=000000&titleColor=FF9900&borderColor=555555&linkColor=FFCC00&borderLinkColor=000000&showMap=0&currentPlayersHeight=280&showCurrPlayers=1&showTopPlayers=0&showBlogs=0&width=160";
        private String ts3UrlFormat = "ts3server://{0}/?port={1}", ts3Target;
        private ListViewItem item;
        private Button btn, btn2, btnag, btnrg, btnam, btnrm;
        private Label FriendlyTeamMemberLabel, FriendlyTeamGuildmastersLabel;
        private HyperLink TeamspeakIpFriendlyTeamLink;
        private IFrameControl TSViewerIFrameControl;
        private Panel OneTeamPanel;
        private FranceTriple.Dal.FriendlyTeam ThisTeam = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!ThisMaster.RequesterIsAuthenticated)
            {
                FriendlyTeamEntityDataSource.WhereParameters.Add(new Parameter("IsPublicVisibleFriendlyTeam",System.Data.DbType.Boolean,"true"));
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {

        }

        protected void FriendlyTeamListView_DataBound(object sender, EventArgs e)
        {
            if (!ThisMaster.RequesterRoles.Contains("RoleEveningChief"))
            {
                FriendlyTeamListView.InsertItem.Visible = false;
                AddGuildMasterPanel.Visible = false;
                RemoveGuildMasterPanel.Visible = false;
                AddMemberPanel.Visible = false;
                RemoveMemberPanel.Visible = false;
            }
        }

        protected void FriendlyTeamListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            item = e.Item;
            if (item.ItemType == ListViewItemType.DataItem)
            {
                ThisTeam = (FranceTriple.Dal.FriendlyTeam)item.DataItem;

                if (ThisMaster.RequesterRoles.Contains("RoleEveningChief"))
                {
                    btn = item.FindControl("DeleteButton") as Button;
                    btn2 = item.FindControl("EditButton") as Button;
                    if (btn != null)
                        btn.Visible = true;
                    if (btn2 != null)
                        btn2.Visible = true;

                    // Les 4 boutons sont visibles true et enable false pour éviter un bug des popupcontrolextender
                    btnag = item.FindControl("AddGuildMasterButton") as Button;
                    btnrg = item.FindControl("RemoveGuildMasterButton") as Button;
                    btnam = item.FindControl("ManageMemberButton") as Button;
                    btnrm = item.FindControl("RemoveMemberButton") as Button;
                    if (btnag != null)
                        btnag.Visible = true;
                    if (btnrg != null)
                        btnrg.Visible = true;
                    if (btnam != null)
                        btnam.Visible = true;
                    if (btnrm != null)
                        btnrm.Visible = true;
                }

                FriendlyTeamMemberLabel = item.FindControl("FriendlyTeamMemberLabel") as Label;
                if (FriendlyTeamMemberLabel != null)
                {
                    FriendlyTeamMemberLabel.Text = String.Join(", ", FriendlyTeamDal.GetTeamMemberNames(ThisTeam.Id));
                }

                FriendlyTeamGuildmastersLabel = item.FindControl("FriendlyTeamGuildmastersLabel") as Label;
                if (FriendlyTeamGuildmastersLabel != null)
                {
                    FriendlyTeamGuildmastersLabel.Text = String.Join(", ", FriendlyTeamDal.GetTeamGuildMasterNames(ThisTeam.Id));
                }

                if (ThisTeam.TeamspeakIpFriendlyTeam != null && ThisTeam.TeamspeakIpFriendlyTeam != String.Empty)
                {
                    TeamspeakIpFriendlyTeamLink = item.FindControl("TeamspeakIpFriendlyTeamLink") as HyperLink;
                    if (TeamspeakIpFriendlyTeamLink != null)
                    {
                        try
                        {
                            ts3Target = String.Format(ts3UrlFormat,
                                ThisTeam.TeamspeakIpFriendlyTeam.Substring(0, ThisTeam.TeamspeakIpFriendlyTeam.LastIndexOf(":")),
                                ThisTeam.TeamspeakIpFriendlyTeam.Substring(ThisTeam.TeamspeakIpFriendlyTeam.LastIndexOf(":") + 1));
                        }
                        catch
                        {
                            //En cas de données ne contenant pas de ":" le champ est vide.
                            //Autrement l'affichage de la page entière se retrouve bloqué.
                            ts3Target = String.Empty;
                        }
                        TeamspeakIpFriendlyTeamLink.NavigateUrl = ts3Target;
                    }
                    //TSViewerIFrameControl = item.FindControl("TSViewerIFrameControl") as IFrameControl;
                    //if (TSViewerIFrameControl != null)
                    //{
                    //    TSViewerIFrameControl.Src = String.Format(TsViewerFormat, ThisTeam.TeamspeakIpFriendlyTeam);
                    //}
                }
            }
            //if (FriendlyTeamListView.EditItem != null)
            //    FriendlyTeamListView.EditItem.Focus();
        }

        protected void FriendlyTeamListView_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            //AddMemberPanel.Visible = true;
            //RemoveMemberPanel.Visible = true;
            //AddGuildMasterPanel.Visible = true;
            //RemoveGuildMasterPanel.Visible = true;

            // Identifier la team cible
            var IdLabel1 = (Label)FriendlyTeamListView.Items[e.NewEditIndex].FindControl("IdLabel1");

            if (IdLabel1 != null)
            {
                //var btnag = (Button)FriendlyTeamListView.Items[e.NewEditIndex].FindControl("AddGuildMasterButton");
                //var btnrg = (Button)FriendlyTeamListView.Items[e.NewEditIndex].FindControl("RemoveGuildMasterButton");
                //var btnam = (Button)FriendlyTeamListView.Items[e.NewEditIndex].FindControl("ManageMemberButton");
                //var btnrm = (Button)FriendlyTeamListView.Items[e.NewEditIndex].FindControl("RemoveMemberButton");
                //btnag.Enabled = true;
                //btnrg.Enabled = true;
                //btnam.Enabled = true;
                //btnrm.Enabled = true;

                AddMemberObjectDataSource.SelectParameters["TeamId"].DefaultValue = IdLabel1.Text;
                RemoveMemberObjectDataSource.SelectParameters["TeamId"].DefaultValue = IdLabel1.Text;
                AddGuildMasterObjectDataSource.SelectParameters["TeamId"].DefaultValue = IdLabel1.Text;
                RemoveGuildMasterObjectDataSource.SelectParameters["TeamId"].DefaultValue = IdLabel1.Text;
            }
        }

        protected void FriendlyTeamListView_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {

            }
        }

        protected void FriendlyTeamListView_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            if (!this.Page.IsValid)
            {
                e.Cancel = true;
                return;
            }
        }

        protected void FriendlyTeamListView_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            if (!this.Page.IsValid)
            {
                e.Cancel = true;
                return;
            }
        }

        protected void FriendlyTeamListView_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
        {
            //AddMemberPanel.Visible = false;
            //RemoveMemberPanel.Visible = false;
            //AddGuildMasterPanel.Visible = false;
            //RemoveGuildMasterPanel.Visible = false;
        }

        protected void AddMemberGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 FriendlyTeamId = 0;
            Guid FriendlyMemberId;
            Label FriendlyTeamMemberLabel;

            // Identifier la team cible
            var IdLabel1 = (Label)FriendlyTeamListView.EditItem.FindControl("IdLabel1");
            if (IdLabel1 != null)
                FriendlyTeamId = Int32.Parse(IdLabel1.Text);

            //Identifier le membre et l'ajouter dans l'équipe
            FriendlyMemberId = Guid.Parse(AddMemberGridView.SelectedValue.ToString());
            if (FriendlyTeamId > 0)
                FriendlyTeamDal.AddMemberToFriendlyTeam(FriendlyTeamId, FriendlyMemberId);

            //Mettre à jour la liste des membres dans l'object en cours d'édition
            FriendlyTeamMemberLabel = FriendlyTeamListView.EditItem.FindControl("FriendlyTeamMemberLabel") as Label;
            if (FriendlyTeamMemberLabel != null)
            {
                FriendlyTeamMemberLabel.Text = String.Join(", ", FriendlyTeamDal.GetTeamMemberNames(FriendlyTeamId));
            }

        }

        protected void RemoveMemberGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 FriendlyTeamId = 0;
            Guid FriendlyMemberId;
            Label FriendlyTeamMemberLabel;

            // Identifier la team cible
            var IdLabel1 = (Label)FriendlyTeamListView.EditItem.FindControl("IdLabel1");
            if (IdLabel1 != null)
                FriendlyTeamId = Int32.Parse(IdLabel1.Text);

            //Identifier le membre et le retirer de l'équipe
            FriendlyMemberId = Guid.Parse(RemoveMemberGridView.SelectedValue.ToString());
            if (FriendlyTeamId > 0)
                FriendlyTeamDal.RemoveMemberFromFriendlyTeam(FriendlyTeamId, FriendlyMemberId);

            //Mettre à jour la liste des membres dans l'object en cours d'édition
            FriendlyTeamMemberLabel = FriendlyTeamListView.EditItem.FindControl("FriendlyTeamMemberLabel") as Label;
            if (FriendlyTeamMemberLabel != null)
            {
                FriendlyTeamMemberLabel.Text = String.Join(", ", FriendlyTeamDal.GetTeamMemberNames(FriendlyTeamId));
            }

        }

        protected void AddGuildMasterGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 FriendlyTeamId = 0;
            Guid FriendlyMemberId;
            Label FriendlyTeamGuildmastersLabel;

            // Identifier la team cible
            var IdLabel1 = (Label)FriendlyTeamListView.EditItem.FindControl("IdLabel1");
            if (IdLabel1 != null)
                FriendlyTeamId = Int32.Parse(IdLabel1.Text);

            //Identifier l'organisateur et l'ajouter dans l'équipe
            FriendlyMemberId = Guid.Parse(AddGuildMasterGridView.SelectedValue.ToString());
            if (FriendlyTeamId > 0)
                FriendlyTeamDal.AddGuildMasterToFriendlyTeam(FriendlyTeamId, FriendlyMemberId);

            //Mettre à jour la liste des guildmasters dans l'object en cours d'édition
            FriendlyTeamGuildmastersLabel = FriendlyTeamListView.EditItem.FindControl("FriendlyTeamGuildmastersLabel") as Label;
            if (FriendlyTeamGuildmastersLabel != null)
            {
                FriendlyTeamGuildmastersLabel.Text = String.Join(", ", FriendlyTeamDal.GetTeamGuildMasterNames(FriendlyTeamId));
            }
        }

        protected void RemoveGuildMasterGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 FriendlyTeamId = 0;
            Guid FriendlyMemberId;
            Label FriendlyTeamGuildmastersLabel;

            // Identifier la team cible
            var IdLabel1 = (Label)FriendlyTeamListView.EditItem.FindControl("IdLabel1");
            if (IdLabel1 != null)
                FriendlyTeamId = Int32.Parse(IdLabel1.Text);

            //Identifier l'organisateur et le retirer de l'équipe
            FriendlyMemberId = Guid.Parse(RemoveGuildMasterGridView.SelectedValue.ToString());
            if (FriendlyTeamId > 0)
                FriendlyTeamDal.RemoveGuildMasterFromFriendlyTeam(FriendlyTeamId, FriendlyMemberId);

            //Mettre à jour la liste des guildmasters dans l'object en cours d'édition
            FriendlyTeamGuildmastersLabel = FriendlyTeamListView.EditItem.FindControl("FriendlyTeamGuildmastersLabel") as Label;
            if (FriendlyTeamGuildmastersLabel != null)
            {
                FriendlyTeamGuildmastersLabel.Text = String.Join(", ", FriendlyTeamDal.GetTeamGuildMasterNames(FriendlyTeamId));
            }
        }
    }
}